﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
    public class Plane
    {
        const float speed = 500;
        const float size = 32;

        public Vector2 pos;
        public Color color;

        Delay delayBullet = new Delay(1.0f / 5.0f);
        int waveIndex = 0;
        Lazor currentLazor;
        Sprite spr = new Sprite();
        Sprite sprHitBox = new Sprite();
        float invulnerable = 1;
        float bulletColorTimer = 0;

        public Rectangle ComputeRect()
        {
            int x = (int)(pos.X - size * 0.5f);
            int y = (int)(pos.Y - size * 0.5f);
            return new Rectangle(x, y, (int)size, (int)size);
        }

        public void Update(Timer timer)
        {
            invulnerable -= timer.Delta;
            bulletColorTimer += timer.Delta;

            float x = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X;
            float y = -GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y;
            bool bulletButtonPressed = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed;
            bool lazorButtonPressed = GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed;
            bool freezeButtonPressed = GamePad.GetState(PlayerIndex.One).Triggers.Right > 0.1f;

            if (freezeButtonPressed)
            {
                lazorButtonPressed = false;
                bulletButtonPressed = false;
            }

            if (lazorButtonPressed)
                bulletButtonPressed = false;

            float localSpeed = speed;

            if (bulletButtonPressed)
                localSpeed *= 0.666f;

            if (lazorButtonPressed)
                localSpeed *= 0.333f;

            if (bulletButtonPressed == false)
                waveIndex = 0;

            float s = localSpeed * timer.Delta;
            pos += new Vector2(x * s, y * s);

            ClipPosition();


            bool okBulletTimer = delayBullet.Update(timer);

            //Color [] bulletsColors = new Color[] { Palette.planeWeaponColor0, Palette.planeWeaponColor1, Palette.planeWeaponColor2 };

            if (bulletButtonPressed && okBulletTimer)
            {
                for (int j = -1; j <= 1; j++)
                {
                    for (int i = -2; i <= 2; i++)
                    {
                        Bullet b = new Bullet();
                        b.radius = 20;
                        b.size = new Vector2(24, 60);
                        Matrix rot = Matrix.CreateRotationZ(Microsoft.Xna.Framework.MathHelper.ToRadians(j * 16));
                        Vector3 up = new Vector3(0, -1, 0);
                        Vector3 off = new Vector3(i * 20.0f, Math.Abs(i) * 12.0f, 0.0f);
                        Vector3 startPos = Vector3.Transform(off, rot);
                        Vector3 dir = Vector3.Transform(up, rot);

                        b.pos = new Vector2(startPos.X, startPos.Y) + pos;
                        b.dir = new Vector2(dir.X, dir.Y);
                        b.speed = 1500;
                        b.damage = 3;

                        //int colorIdx = (int)Math.Abs(i);
                        
                        Color bulletColor = Color.Lerp(Palette.planeWeaponColor0, Palette.planeWeaponColor2, (float) Math.Sin(bulletColorTimer * 10.0f) * 0.5f + 0.5f);
                        b.color0 = bulletColor;
                        b.color1 = bulletColor;
                        Game1.instance.bulletManager.bullets.Add(b);
                    }
                }

                delayBullet.DelayDuration = (1.0f / 15.0f);
                waveIndex++;

//                 if (waveIndex == 5)
//                 {
//                     waveIndex = 0;
//                     delayBullet.DelayDuration = (3.0f / 10.0f);
//                 }

                delayBullet.Restart();
            }

            if (lazorButtonPressed)
            {
                if (currentLazor == null)
                {
                    currentLazor = new Lazor();
                    Game1.instance.bulletManager.lazors.Add(currentLazor);
                    currentLazor.top = pos.Y;
                }
                currentLazor.alive = true;
                currentLazor.bottom = pos.Y;
                currentLazor.origin = pos;
            }
            else
            {
                if (currentLazor != null)
                    currentLazor.alive = false;
                currentLazor = null;
            }

            if (freezeButtonPressed)
            {
                Bullet b = new Bullet();
                b.size = new Vector2(128, 16);
                b.pos = pos;
                b.dir = new Vector2(0, -1);
                b.speed = 1250;
                b.color0 = Color.PowderBlue;
                b.color1 = Color.PowderBlue;
                Game1.instance.bulletManager.freezeBullets.Add(b);
            }


        }

        /// <summary>
        /// Make the plane stay within the screen limits.
        /// </summary>
        public void ClipPosition()
        {
            if (pos.X - size * 0.5f < 0)
                pos.X = size * 0.5f;

            if (pos.X + size * 0.5f > Game1.width)
                pos.X = Game1.width - size * 0.5f;

            if (pos.Y - size * 0.5f < 0)
                pos.Y = size * 0.5f;

            if (pos.Y + size * 0.5f > Game1.height)
                pos.Y = Game1.height - size * 0.5f;
        }

        public void Draw(SpriteBatch sb)
        {
            spr.pos = pos;
            spr.color = color;
            if (invulnerable > 0)
            {
                
                spr.color = Color.Lerp( color, Color.Transparent, (float)Math.Sin(invulnerable * 8) * 0.4f + 0.5f);
            }
            spr.Draw(sb);
            sprHitBox.pos = pos;
            sprHitBox.Draw(sb);
        }

        public Plane()
        {
            color = new Color(0x0FF, 0x0E7, 0x057);
            pos = new Vector2(Game1.width / 2, Game1.height - 64);

            spr.pos = new Vector2(300, 900);
            spr.size = new Vector2(size, size);
            spr.origin = new Vector2(0.5f, 0.5f);

            sprHitBox.size = new Vector2(4, 4);
            sprHitBox.origin = new Vector2(0.5f, 0.5f);
            sprHitBox.color = Color.White;

            invulnerable = 2;

        }

        public void OnEventHit()
        {
            if (invulnerable > 0)
                return;

            Game1.instance.explosionManager.AddExplosionBig(pos);
            Game1.instance.lifeCount--;
            if (Game1.instance.lifeCount > 0)
            {                
                invulnerable = 2;
            }
        }

    }
}
